package home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.bumptech.glide.Glide;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import base.BasePagerAdapter;
import base.InfiniteViewPager;
import widget.BottomIndicator;

/**
 * @author BF00233
 */

public class TopBannerAdapter extends DelegateAdapter.Adapter<TopBannerAdapter.BannerViewHolder> {

    private static final String TAG = "BannerAdapter";

    private final Context mContext;

    private List<String> mData;

    public TopBannerAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setUpBanner(List<String> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new SingleLayoutHelper();
    }

    @NonNull
    @Override
    public BannerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_home_top_banner, parent, false);
        return new BannerViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final BannerViewHolder holder, int position) {
        if (mData == null) return;
//        BannerPagerAdapter adapter = new BannerPagerAdapter(mData.size());
//        holder.viewPager.setAdapter(adapter);
//        holder.bottomIndicator.setUpWithViewPager(holder.viewPager, true);
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        private InfiniteViewPager viewPager;
        private BottomIndicator bottomIndicator;
        private ImageView topbackimg;

        private BannerViewHolder(View itemView) {
            super(itemView);
//            bottomIndicator = itemView.findViewById(R.id.bottom_indicator);
//            viewPager = itemView.findViewById(R.id.view_pager);
            // 多于一张图时，才自动滚动
//            viewPager.setAutoScroll(mData.size() > 1);
        }
    }

    private class BannerPagerAdapter extends BasePagerAdapter {

        BannerPagerAdapter(int count) {
            super(count);
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, final int index) {
            ImageView imageView = new ImageView(container.getContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(container.getContext())
                    .load(mData.get(index))
                    .into(imageView);
            container.addView(imageView);
            return imageView;
        }
    }

}
